using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using EntLibEC.Cmn;
using EntLibEC.Info;
using EntLibEC.Info.Sale;
using EntLibEC.Biz;
using EntLibEC.Biz.Sale;

namespace EntLibEC.View.Sale
{
	/// <summary>
	/// Summary description for SOVAT.
	/// </summary>
	public partial class SOVAT : PageBase
	{
	
		private SOInfo soInfo ;
		protected string bodyOnLoad = "";
		protected void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			this.GetInfo();
			if(!IsPostBack)
			{				
				this.InitPage();
			}
			this.btnClose.Attributes.Add("onClick","javascript:window.close();");
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.myGrid.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.myGrid_ItemCommand);

		}
		#endregion

		private void GetInfo()
		{
			if(this.sInfo.sSO==null)
				Response.Redirect("SODetail.aspx");
			soInfo = this.sInfo.sSO;
		}

		private void InitPage()
		{
			if(soInfo.VatInfo.BankAccount!=AppConst.StringNull)
			{
				this.txtBankAccount.Text = soInfo.VatInfo.BankAccount;
				this.txtCompanyAddress.Text = soInfo.VatInfo.CompanyAddress;
				this.txtCompanyName.Text = soInfo.VatInfo.CompanyName;
				this.txtCompanyPhone.Text = soInfo.VatInfo.CompanyPhone;
				this.txtMemo.Text = soInfo.VatInfo.Memo;
				this.txtTaxSignNo.Text = soInfo.VatInfo.TaxNum;
			}
			if(soInfo.Status==(int)AppEnum.SOStatus.Origin)
			{
				this.btnSave.Visible = true;
				this.btnSave.Enabled = true;
			}
			else
			{
				this.btnSave.Visible = false;
				this.btnSave.Enabled = false;
			}
			this.BindGrid();
		}

		private void BindGrid()
		{
			Hashtable vatHash = SOManager.GetInstance().GetDistinctVATList(soInfo.CustomerSysNo);
			ViewState["vatHash"] = vatHash;
			myGrid.DataSource = this.BuildTable(vatHash);
			myGrid.DataBind();
		}

		private DataTable BuildTable(Hashtable vatHash)
		{
			DataTable dt = new DataTable();
			DataColumn dc0 = new DataColumn("SysNo",typeof(System.Int32));
			DataColumn dc1 = new DataColumn("CompanyName",typeof(System.String));
			DataColumn dc2 = new DataColumn("TaxNum",typeof(System.String));
			DataColumn dc3 = new DataColumn("BankAccount",typeof(System.String));
			DataColumn dc4 = new DataColumn("CompanyAddress",typeof(System.String));
			DataColumn dc5 = new DataColumn("CompanyPhone",typeof(System.String));
			dt.Columns.Add(dc0);
			dt.Columns.Add(dc1);
			dt.Columns.Add(dc2);
			dt.Columns.Add(dc3);
			dt.Columns.Add(dc4);
			dt.Columns.Add(dc5);
			foreach(SOVATInfo vatInfo in vatHash.Values)
			{
				DataRow dr = dt.NewRow();
				dr["SysNo"] = vatInfo.SysNo;
				dr["CompanyName"] = vatInfo.CompanyName;
				dr["TaxNum"] = vatInfo.TaxNum;
				dr["BankAccount"] = vatInfo.BankAccount;
				dr["CompanyAddress"] = vatInfo.CompanyAddress;
				dr["CompanyPhone"] = vatInfo.CompanyPhone;
				dt.Rows.Add(dr);
			}
			return dt;
		}

		private void myGrid_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			if(e.CommandName=="Select")
			{
				SOVATInfo vatInfo = (SOVATInfo)((Hashtable)ViewState["vatHash"])[(int)myGrid.DataKeys[(int)e.Item.ItemIndex]];
				this.txtCompanyName.Text = vatInfo.CompanyName;
				this.txtTaxSignNo.Text = vatInfo.TaxNum;
				this.txtCompanyAddress.Text = vatInfo.CompanyAddress;
				this.txtCompanyPhone.Text = vatInfo.CompanyPhone;
				this.txtBankAccount.Text = vatInfo.BankAccount;
			}
		}

		protected void btnSave_Click(object sender, System.EventArgs e)
		{
			if(this.txtBankAccount.Text.Trim()==string.Empty)
			{
				Assert(lblmsg,"Bank&Account can't be null",-1);
				return;
			}
			else if(this.txtCompanyAddress.Text.Trim()==string.Empty)
			{
				Assert(lblmsg,"CompanyAddress can't be null",-1);
				return;
			}
			else if(this.txtCompanyName.Text.Trim()==string.Empty)
			{
				Assert(lblmsg,"CompanyName can't be null",-1);
				return;
			}
			else if(this.txtCompanyPhone.Text.Trim()==string.Empty)
			{
				Assert(lblmsg,"CompanyPhone can't be null",-1);
				return;
			}
			else if(this.txtTaxSignNo.Text.Trim()==string.Empty)
			{
				Assert(lblmsg,"TaxSignNo can't be null",-1);
				return;
			}
			SOVATInfo vatInfo = new SOVATInfo();
			vatInfo.SOSysNo = soInfo.SysNo;
			vatInfo.CustomerSysNo = soInfo.CustomerSysNo;
			vatInfo.CompanyName = txtCompanyName.Text.Trim();
			vatInfo.CompanyAddress = txtCompanyAddress.Text.Trim();
			vatInfo.CompanyPhone = txtCompanyPhone.Text.Trim();
			vatInfo.BankAccount = txtBankAccount.Text.Trim();
			vatInfo.TaxNum = txtTaxSignNo.Text.Trim();
			vatInfo.Memo = txtMemo.Text.Trim();
			if(soInfo.SysNo!=AppConst.IntNull)
			{
				try
				{
					SOManager.GetInstance().SaveVAT(vatInfo);
				}
				catch(BizException be)
				{
					Assert(lblmsg,be.Message,-1);
					return;
				}
				catch(Exception ex)
				{
					Assert(lblmsg,ex.Message,-1);
					return;
				}
			}
			this.sInfo.sSO.VatInfo = vatInfo;
		}
	}
}
